package com.lyl.momanapp.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lljjcoder.city_20170724.CityPickerView;
import com.lljjcoder.city_20170724.bean.CityBean;
import com.lljjcoder.city_20170724.bean.DistrictBean;
import com.lljjcoder.city_20170724.bean.ProvinceBean;
import com.lyl.momanapp.R;
import com.lyl.momanapp.bean.URL;
import com.lyl.momanapp.bean.User;
import com.lyl.momanapp.util.utlis.InterfaceUtils;
import com.lyl.momanapp.util.utlis.MyUtils;
import com.lyl.momanapp.util.view.MyBaseActivity;

import java.util.HashMap;

/**
 * 名字 梁亚龙
 * 时间 2017/9/15.
 * QQ邮箱 503934353@qq.com
 * 添加收货地址页面
 */

public class UserAddressActivity extends MyBaseActivity {


    private RelativeLayout mRL;
    private CityPickerView mCityPickerView;
    private TextView mCity;
    private Button mBut;
    private EditText mName, mPhone, mAddress;
    private String mCountry = "中国", mProv, mCityURL, mArea;
    private boolean state = false;
    private User.listAddress.ResultBean resultBean;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case URL.ID_ADD_ADDRESS: {
                    final User.addAddress addAddress = new Gson().fromJson(msg.obj.toString(), User.addAddress.class);
                    if (addAddress.getState().equals("M00000")) {
                        Toast.makeText(UserAddressActivity.this, addAddress.getMessage(), Toast.LENGTH_SHORT).show();
                        new MyUtils().alertDialogPrompt(UserAddressActivity.this, "提示", "是否设置默认地址", true, new InterfaceUtils.PromptCallback() {
                            @Override
                            public void onPositiveButton() {
                                HashMap<String, String> map = new HashMap<String, String>();
                                map.put("user_id", URL.Userlogin.getResult().getUser_id() + "");
                                map.put("address_id", addAddress.getResult().getAddress_id() + "");
                                new MyUtils().xUtilsPost(URL.URL_SET_DEFAULT, map, handler, URL.ID_SET_DEFAULT);
                            }

                            @Override
                            public void onNegativeButton() {
                                UserAddressActivity.this.finish();
                            }
                        });
                    } else {
                        Toast.makeText(UserAddressActivity.this, addAddress.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                break;
                case URL.ID_SET_DEFAULT: {
                    User.setAddress setAddress = new Gson().fromJson(msg.obj.toString(), User.setAddress.class);
                    if (setAddress.getState().equals("M00000")) {
                        Toast.makeText(UserAddressActivity.this, setAddress.getMessage(), Toast.LENGTH_SHORT).show();
                        UserAddressActivity.this.finish();
                    } else {
                        Toast.makeText(UserAddressActivity.this, setAddress.getMessage(), Toast.LENGTH_SHORT).show();
                        UserAddressActivity.this.finish();
                    }
                }
                break;
                case URL.ID_EDIT_ADDRESS: {
                    User.editAddress editAddress = new Gson().fromJson(msg.obj.toString(), User.editAddress.class);
                    if (editAddress.getState().equals("M00000")) {
                        Toast.makeText(UserAddressActivity.this, editAddress.getMessage(), Toast.LENGTH_SHORT).show();
                        UserAddressActivity.this.finish();
                    } else {
                        Toast.makeText(UserAddressActivity.this, editAddress.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            }
        }
    };

    @Override
    public void initUI() {
        setContentView(R.layout.activity_user_address);
        mRL = ((RelativeLayout) findViewById(R.id.address_rl));
        mCity = ((TextView) findViewById(R.id.address_city_tv));
        mBut = ((Button) findViewById(R.id.address_but));
        mName = ((EditText) findViewById(R.id.address_name));
        mPhone = ((EditText) findViewById(R.id.address_phone));
        mAddress = ((EditText) findViewById(R.id.address_address));

    }

    @Override
    public void initData() {
        resultBean = getIntent().getParcelableExtra("resultBean");
        if (resultBean == null) {
            state = false;
            Log.i("111", "initData: " + 111);
        } else {
            state = true;
            Log.i("111", "initData: " + resultBean.toString());
            mCity.setText(resultBean.getProv() + resultBean.getCity() + resultBean.getArea());
            mName.setText(resultBean.getConsignee());
            mPhone.setText(resultBean.getPhone());
            mAddress.setText(resultBean.getAddress());
        }
        if (URL.Userlogin==null){
            startActivity(new Intent(this,UserLoginActivity.class));
        }

    }

    @Override
    public void addListeners() {
        mBut.setOnClickListener(this);
        mRL.setOnClickListener(this);
        mCityPickerView = new MyUtils().cityPickerView(UserAddressActivity.this);
        mCityPickerView.setOnCityItemClickListener(new CityPickerView.OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean provinceBean, CityBean cityBean, DistrictBean districtBean) {
                mProv = provinceBean.getName();
                mCityURL = cityBean.getName();
                mArea = districtBean.getName();
                if (state) {
                    resultBean.setProv(provinceBean.getName());
                    resultBean.setCity(cityBean.getName());
                    resultBean.setArea(districtBean.getName());
                }
                mCity.setText(provinceBean.getName() + cityBean.getName() + districtBean.getName());
            }
            @Override
            public void onCancel() {

            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.address_rl: {

                mCityPickerView.show();
            }
            break;
            case R.id.address_but: {
                if (state) {
                    if (TextUtils.isEmpty(mName.getText()) || TextUtils.isEmpty(mPhone.getText()) ||
                            TextUtils.isEmpty(mAddress.getText()) || TextUtils.isEmpty(mCity.getText())
                            || resultBean.getProv() == null || resultBean.getCity() == null || resultBean.getArea() == null
                            ) {
                        Toast.makeText(UserAddressActivity.this, "不能为空", Toast.LENGTH_SHORT).show();
                    } else {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("user_id", URL.Userlogin.getResult().getUser_id() + "");
                        map.put("address_id", resultBean.getAddress_id() + "");
                        map.put("consignee", mName.getText().toString());
                        map.put("phone", mPhone.getText().toString());
                        map.put("address", mAddress.getText().toString());
                        map.put("country", mCountry);
                        map.put("prov", resultBean.getProv());
                        map.put("city", resultBean.getCity());
                        map.put("area", resultBean.getArea());
                        new MyUtils().xUtilsPost(URL.URL_EDIT_ADDRESS, map, handler, URL.ID_EDIT_ADDRESS);
                    }


                } else {
                    if (TextUtils.isEmpty(mName.getText()) || TextUtils.isEmpty(mPhone.getText()) ||
                            TextUtils.isEmpty(mAddress.getText()) || TextUtils.isEmpty(mCity.getText())
                            || mProv == null || mCityURL == null || mArea == null
                            ) {
                        Toast.makeText(UserAddressActivity.this, "不能为空", Toast.LENGTH_SHORT).show();
                    } else {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("user_id", URL.Userlogin.getResult().getUser_id() + "");
                        map.put("consignee", mName.getText().toString());
                        map.put("phone", mPhone.getText().toString());
                        map.put("address", mAddress.getText().toString());
                        map.put("country", mCountry);
                        map.put("prov", mProv);
                        map.put("city", mCityURL);
                        map.put("area", mArea);
                        new MyUtils().xUtilsPost(URL.URL_ADD_ADDRESS, map, handler, URL.ID_ADD_ADDRESS);
                    }

                }
            }
            break;

        }
    }


}
